using System;

namespace FrameIdentityReq.Frames
{
    public class DateTimeField : BitsField, IDateTimeField
    {
        public IIntField Year { get; set; }
        public IIntField Month { get; set; }
        public IIntField Day { get; set; }
        public IIntField Hour { get; set; }
        public IIntField Minute { get; set; }
        public IIntField Second { get; set; }

        public DateTimeField(IBytesBuffer buffer, int index, string name)
            : base(buffer, index, 4 << 3, name)
        {
            Year = new IntField(buffer, index, 6, "Year");
            Month = new IntField(Year, 4, "Month");
            Day = new IntField(Month, 5, "Day");
            Hour = new IntField(Day, 5, "Hour");
            Minute = new IntField(Hour, 6, "Minute");
            Second = new IntField(Minute, 6, "Second");
        }

        public DateTimeField(IField field, string name)
            : this(field, field.Index + field.Count, name) { }

        public new DateTime Value
        {
            get
            {
                DateTime dateTime = new DateTime(Year.Value + 2010, Month.Value, Day.Value, Hour.Value, Minute.Value, Second.Value);
                return dateTime;
            }

            set
            {

                Year.Value = value.Year - 2010;
                Month.Value = value.Month;
                Day.Value = value.Day;
                Hour.Value = value.Hour;
                Minute.Value = value.Minute;
                Second.Value = value.Second;
            }
        }
    }
}